﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		RssResult.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Dexter.Domain.Contracts;
using Dexter.Domain.Model;
using Dexter.Services;
using Dexter.Web.Syndication.Contracts;
using Dexter.Web.Syndication.Model;

namespace Dexter.Web.Mvc.Results {
	public class RssResult : ActionResult {
		readonly SiteConfiguration configuration;
		readonly IEnumerable <IFeedItem> items;
		readonly IRssService rssService;
		readonly IUrlBuilder urlBuilder;

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "RssResult" /> class.
		/// </summary>
		/// <param name = "configuration">The configuration.</param>
		/// <param name = "items">The items.</param>
		/// <param name = "rssService">The RSS service.</param>
		/// <param name = "urlBuilder">The URL builder.</param>
		public RssResult ( SiteConfiguration configuration , IEnumerable <IFeedItem> items , IRssService rssService , IUrlBuilder urlBuilder ) {
			this.configuration = configuration;
			this.urlBuilder = urlBuilder;
			this.rssService = rssService;
			this.items = items;
		}

		/// <summary>
		/// 	Enables processing of the result of an action method by a custom type that inherits from the <see cref = "T:System.Web.Mvc.ActionResult" /> class.
		/// </summary>
		/// <param name = "context">The context in which the result is executed. The context information includes the controller, HTTP content, request context, and route data.</param>
		public override void ExecuteResult ( ControllerContext context ) {
			var rssHeader = new RssHeader ( );
			rssHeader.Categories = configuration.SeoConfiguration.DefaultKeyWords;
			rssHeader.Copyright = configuration.SeoConfiguration.CopyRight;
			rssHeader.Culture = configuration.Culture;
			rssHeader.Description = configuration.SeoConfiguration.DefaultDescription;
			rssHeader.EditorName = configuration.PrincipalAuthor;
			rssHeader.LatestNewsDate = items.Min ( x => x.FeedItemPublishDateUtc );
			rssHeader.Link = urlBuilder.HomePage;
			rssHeader.Title = configuration.SeoConfiguration.DefaultTitle;

			var rssItems = items.Select ( x => new RssItem ( ) {
				Author = x.FeedItemAuthor ,
				Categories = x.FeedItemCategories ,
				CommentsLink = new Uri ( urlBuilder.Post.FeedBack ( x.FeedItemOwner ) ) ,
				Description = x.FeedItemDescription ,
				Link = new Uri ( urlBuilder.Post.Detail ( x.FeedItemOwner ) ) ,
				PublishDate = x.FeedItemPublishDateUtc ,
				Title = x.FeedItemTitle
			} ).ToArray ( );

			rssService.CreateHeader ( rssHeader )
				.AddItems ( rssItems )
				.Save ( context.HttpContext.Response.OutputStream );
		}
	}
}
